<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="客户名称:">
        <el-input v-model="queryParams.customerName" size="small" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['accounting:acctCheckConfig:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['accounting:acctCheckConfig:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['accounting:acctCheckConfig:export']" type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户" align="center" prop="customerName" />
      <el-table-column label="配置类型" align="center" prop="configType" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.configType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间类型" align="center" prop="timeType" />
      <el-table-column label="字段列表" align="center" prop="fields" />
      <el-table-column label="字段数量" align="center" prop="fieldCount" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['accounting:acctCheckConfig:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['accounting:acctCheckConfig:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="acctCheckConfigFormDialog.title" :visible.sync="acctCheckConfigFormDialog.visible" :close-on-click-modal="false" :before-close="resetAcctCheckConfigFormDialog" width="725px" append-to-body>
      <el-form ref="acctCheckConfigForm" :model="acctCheckConfigFormData" :rules="acctCheckConfigFormRules" label-width="80px">
        <el-form-item label="客户" prop="customerId">
          <el-select v-model="acctCheckConfigFormData.customerId" size="small" style="width: 100%" placeholder="客户" clearable>
            <el-option v-for="option in options.customerOptions" :key="option.id" :label="option.customerName" :value="option.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="配置类型" prop="configType">
          <el-radio-group v-model="acctCheckConfigFormData.configType" @change="getTableColumnList">
            <el-radio v-for="option in $constant.accCheckConfigType" :key="option.key" :label="option.key">{{ option.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="分组类型" prop="groupType">
          <el-radio-group v-model="acctCheckConfigFormData.groupType">
            <el-radio v-for="option in $constant.accGroupType" :key="option.key" :label="option.key">{{ option.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="时间类型" prop="timeType">
          <el-radio-group v-model="acctCheckConfigFormData.timeType">
            <el-radio v-for="option in $constant.accTimeType" :key="option.key" :label="option.key">{{ option.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="字段列表">
          <el-transfer
            v-model="fieldList"
            :props="{ key: 'columnName', label: 'columnComment' }"
            :data="options.tableColumnOptions"
            filterable
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmAcctCheckConfigFormDialog">确 定</el-button>
        <el-button @click="resetAcctCheckConfigFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AcctCheckConfigApi from '@/api/accounting/AcctCheckConfigApi'
import CustomerApi from '@/api/basic/CustomerApi'
import GenTableApi from '@/api/tool/GenTableApi'

const defaultAcctCheckConfigFormData = {
  customerId: null,
  configType: null,
  timeType: null,
  groupType: null,
  fields: null,
  fieldCount: null
}
export default {
  name: 'AcctCheckConfigList',
  data() {
    return {
      options: {
        customerOptions: [],
        tableColumnOptions: []
      },
      queryParams: {
        customerName: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      acctCheckConfigFormDialog: {
        title: '',
        visible: false
      },
      acctCheckConfigFormData: Object.assign({}, defaultAcctCheckConfigFormData),
      acctCheckConfigFormRules: {
        customerId: [{ required: true, message: '客户ID不能为空', trigger: 'blur' }],
        configType: [{ required: true, message: '配置类型不能为空', trigger: 'change' }],
        groupType: [{ required: true, message: '分组类型不能为空', trigger: 'change' }],
        timeType: [{ required: true, message: '时间类型不能为空', trigger: 'change' }]
      },
      fieldList: [],
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      CustomerApi.list().then(response => {
        this.options.customerOptions = response.data
      })
    },
    getTableColumnList() {
      if (this.acctCheckConfigFormData.configType) {
        GenTableApi.getTableColumnList(this.acctCheckConfigFormData.configType).then(response => {
          this.options.tableColumnOptions = response.data
        })
      }
    },
    handleAdd() {
      this.openAcctCheckConfigFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return AcctCheckConfigApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openAcctCheckConfigFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      AcctCheckConfigApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return AcctCheckConfigApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openAcctCheckConfigFormDialog(id) {
      this.acctCheckConfigFormDialog.visible = true
      if (id) {
        this.acctCheckConfigFormDialog.title = '对账单配置-修改'
        AcctCheckConfigApi.get(id).then(response => {
          this.acctCheckConfigFormData = {
            ...this.acctCheckConfigFormData,
            ...response.data
          }
          this.getTableColumnList()
          if (this.acctCheckConfigFormData.fields) {
            this.fieldList = this.acctCheckConfigFormData.fields.split(',')
          }
        })
      } else {
        this.acctCheckConfigFormDialog.title = '对账单配置-添加'
      }
    },
    confirmAcctCheckConfigFormDialog() {
      this.$refs['acctCheckConfigForm'].validate(valid => {
        if (valid) {
          const params = {
            ...this.acctCheckConfigFormData
          }
          params.fields = this.fieldList.join(',')
          params.fieldCount = this.fieldList.length
          console.log(params)
          if (this.acctCheckConfigFormData.id) {
            AcctCheckConfigApi.edit(params).then(response => {
              this.$message.success('修改成功')
              this.resetAcctCheckConfigFormDialog()
              this.handleQuery()
            })
          } else {
            AcctCheckConfigApi.add(params).then(response => {
              this.$message.success('添加成功')
              this.resetAcctCheckConfigFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetAcctCheckConfigFormDialog() {
      this.acctCheckConfigFormDialog = {
        title: '',
        visible: false
      }
      this.acctCheckConfigFormData = Object.assign({}, defaultAcctCheckConfigFormData)
      this.options.tableColumnOptions = []
      this.resetForm('acctCheckConfigForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
